# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: jaewon

import datetime
import logging

from absl import app

from coin.exchange.okex.kr_rest.product import OkexProduct
import experimental.prophet as prophet


def graph():
  ts = prophet.global_timestamp()
  timer_1s = prophet.timer('1s')

  product = OkexProduct.FromStr('BTC-USDT')
  feed = prophet.fastfeed_coin(product, 'Spot.Okex.v3')
  trade = prophet.create_node('fastfeed.Trade', [feed.trade]).outputs

  last_price = prophet.shared(float, shape=prophet.Shape.Scalar())
  new_price = prophet.cond(prophet.isnan(trade.price), last_price, trade.price)
  last_price = prophet.assign(last_price, new_price)

  prophet.printer('%-30t last_price=%s trade_price=%s', ts, last_price, trade.price)

  with prophet.control_if(ts % 10**9 == 0):
    aggregator = prophet.scalar_value_aggregator(
        [ts, last_price.named_ref('last_price'), trade.price.named_ref('trade_price')])
  return aggregator


def main(argv):
  df = prophet.run_from_fastfeed_multiprocess(graph,
                                              datetime.datetime(2019, 9, 1),
                                              datetime.datetime(2019, 9, 1, 0, 10),
                                              machine='feed-01.ap-northeast-1.aws',
                                              use_run_cache=False)
  print(df)


if __name__ == '__main__':
  logging.basicConfig(level='DEBUG')
  app.run(main)
